transito = {
	polylines: [],	
	reloadMap: false,
	
	bindEvents: function(){
		$("#btnTransito").click(function(){
			if (false){
				monitoreo.mostrarNotificacion("Error: ", "top right", "error");
				transito.reloadMap = false;
			} else {
				var idCriterio = $("input[type=radio]:checked").val();
				transito.llamadaAjax(idCriterio);
			}
			$(".navdrawer.navdrawer-hidden").removeClass("is-open");
		});
		
		$(".navdrawer-toggle").click(function(){
			if($(".consultaTransito").val() == "1"){
				var element = $(".navdrawer-toggle").parent().parent();
				var mask = $('.navdrawer-mask');
				
				if(element.hasClass("is-open")){
					element.removeClass("is-open");
					mask.removeClass("opacity");
				}else{
					element.addClass("is-open");
					mask.addClass("opacity");
				}
			}
		});
		
		$(".navdrawer-mask").click(function(){
			var element = $(".navdrawer-toggle").parent().parent();
			var mask = $('.navdrawer-mask');
			if(element.hasClass("is-open")){
				element.removeClass("is-open");
				mask.removeClass("opacity");
			}
		});
		
		$(".monitorioSwitch").bootstrapSwitch();
	},
		
	disableOpacity: function(){
		var element = $(".navdrawer-toggle").parent().parent();
		var mask = $('.navdrawer-mask');
		if(!element.hasClass("is-open")){
			element.removeClass("is-open");
			mask.removeClass("opacity");
		}
	},
	
	mostrarNotificacion: function(str, position, type){
		$.notify(str, { globalPosition: position,
			  		className: type,
			  		clickToHide: true,
			  		autoHide: true,
			  		autoHideDelay: 4000,
		     	  });
	},
	
	llamadaAjax: function(idCriterio){
		var formData = null;
		formData = {"idCriterio": idCriterio};
		request = $.ajax({
    	    url : "DevolverEstadoTransito",
    	    type: "POST",
    	    data : formData
    	});
		for (var i=0; i < transito.polylines.length; i++) {                           
			transito.polylines[i].setMap(null); 
		}
		transito.polylines = [];
		request.done(function (response, textStatus, jqXHR){
			console.log(response);
			transito.disableOpacity();
			if($.isArray(response)){
				if(response.length > 0){
					response.forEach(function(element, index, array){
			  			var ini = element.inicio;
			  			var fin = element.fin;
			  			
			  			var lineCoordinates = [
			  			   new google.maps.LatLng(ini.lat, ini.lon),
			  			   new google.maps.LatLng(fin.lat, fin.lon)
			  			];
			  			line = new google.maps.Polyline({
			  		      path: lineCoordinates,
			  		      strokeColor: element.color,
			  		      strokeOpacity: 1.0,
			  		      strokeWeight: 3
			  		    });
			  			line.setMap(var_map);
			  			transito.polylines.push(line);
			  		});
					transito.reloadMap = true;
				}else{
					transito.mostrarNotificacion("No existen observaciones.","top-right","error");
					transito.reloadMap = false;
				}
		}else{
			transito.mostrarNotificacion("Se ha producido un error. Intente nuevamente.","top-right","error");
			transito.reloadMap = false;
		}
		});
	}
};

$(document).ready(function(){
	$(".transito").addClass("active");
	transito.bindEvents();
	if($(".consultaTransito").val() == "2"){
		$(".navdrawer-toggle").hide();
		transito.llamadaAjax(null);
	}
	
	$('#btnPopover').click(function(){
		$('.form-control').focus();
		$('.account').popover({
	        title: '<span class="popoverTit">Perfil</span>',
	        content: $('.accountHide').html(),
	        placement: 'bottom',
	        html: true
	    });
	});
	
	setInterval(function() {
		 if(transito.reloadMap){
			 if($(".publicUser").val() == "-1"){
				 transito.llamadaAjax(null);
			 }else{
				 $("#btnTransito").click();
			 }
		 }
	}, 60000);
});